/**
 * 游戏内世界排行
 */
class WorldRankUI extends UIBase{
    private btn_close:BaseButton;
    private rank_scroller:eui.Scroller;
    private rank_list:eui.List;

    private _listDataArr:eui.ArrayCollection;
    //当前的页数
	private _curPage:number;

    private curPage:number;

    /**self rank */
    private img_rank:eui.Image;
    private lb_rank:eui.BitmapLabel;
    private img_mask:eui.Image;
    private img_head:eui.Image;
    private lb_name:eui.Label;
    private lb_customs:eui.Label;

    private tab_1:eui.Group;
    private tab_2:eui.Group;
    private img_di1:eui.Image;
    private img_di2:eui.Image;

    private curSelectIndex:number;
    private group_world:eui.Group;

    private rank_group1:eui.Group;
    // private btn_next:BaseButton;
    // private btn_last:BaseButton;
    private img_tittle_1:eui.Image;
    private img_tittle_2:eui.Image;

    private list = [];
    constructor()
    {
        super('WorldRankView',{showBG:true});
    }

    public init()
    {
        super.init(true);
        this.rank_list.itemRenderer = RankItem;

        this._curPage = 1;
        this.curSelectIndex = 1;
        this.postWorldData();
        UIManager.instance().hideLamp();
    }

    // public postSelfData()
	// {
	// 	UIManager.instance().showNormalLoading();
    //     let data = {
    //         version:GameConst.version
    //     }
    //     HttpManager.instance().sendPost(GameConst.httpUrl + 'warriorendless/get_self_rank' ,StringUtil.jsonToHttpPostString(data), (e)=>{
    //         UIManager.instance().hideNormalLoading();
    //         let data = JSON.parse(e);
    //         if(data && data.data)
    //         {
    //             this.updateSelfRank(data.data);
    //             this.postWorldData();
    //         }
    //     })
	// }

    public updateSelfRank(data)
    {
        let avatar = data.avatar_url;
        let score = data.endless_score;
        let customs = data.level;
        let name = data.nick_name;
        let rank = data.rank_id;
        this.img_head.source = avatar;

        if(rank <= 3 && rank >0)
        {
            this.img_rank.source = 'rank_'+rank+'_png';
            this.lb_rank.visible = false;
            this.img_rank.visible = true;
        }
        else
        {
            this.img_rank.visible = false;
            this.lb_rank.visible = true;
        }
        this.lb_name.text = StringUtil.getNameSlice(name);
        
        this.img_head.width = this.img_head.height = 76;
        this.img_head.mask = this.img_mask;
        if(rank == 0)
        {
            this.lb_rank.text = '99+';
        }
        else
        {
            this.lb_rank.text = rank;
        }

        if(score != null)
        this.lb_customs.text = score+'分';
        if(customs != null)
        this.lb_customs.text = `第${customs}关`;
    }

    protected changeUI(data)
    {
        let index = data[0];
        if(index == this.curSelectIndex)return;
        if(this.curSelectIndex == 1)
        {
            this.curSelectIndex = 2;
        }
        else
        {
            this.curSelectIndex = 1;
        }
        let bool = this.curSelectIndex == 1;
        this.img_di1.visible = bool;
        this.img_di2.visible = !bool;
        this.img_tittle_1.source = bool ? 'new_rank_5_png':'new_rank_5_1_png';
        this.img_tittle_2.source = !bool ? 'new_rank_6_png':'new_rank_6_1_png';
        this._curPage = 1;
        this.list = [];
        this._listDataArr = null;
        this.rank_list.dataProvider = null;
		this.rank_scroller.stopAnimation();
		this.rank_scroller.viewport.scrollV = 0;
        this.postWorldData();
    }

    public getRank()
    {
        let bitmap_rank = WeChatgGameManager.instance().createRankListDisplayObject();
        if(bitmap_rank)
        {
            bitmap_rank.name = 'bitmap_rank';
            this.rank_group1.addChild(bitmap_rank);
        }
    }

    public postWorldData()
    {
        let data = {
            version:GameConst.version,
            page:this._curPage,
            type:this.curSelectIndex == 1 ? 'normal' : 'endless'
        }
        // UIManager.instance().showNormalLoading();
        HttpManager.instance().sendPost(GameConst.httpUrl + 'warrioruser/global_rank' ,StringUtil.jsonToHttpPostString(data), (e)=>{
            // UIManager.instance().hideNormalLoading();
            let data = JSON.parse(e);
            if(data && data.data)
            {
                if(data.data.global_rank.length > 0)
                {
                    this.initUI(data.data.global_rank);
                    if(this._curPage == 1)
                    {
                        this.updateSelfRank(data.data.self_rank);
                    }
                    this._curPage++;
                }
            }
        })
    }

    public initUI(data:Array<any>)
    {
        if(this._curPage == 1)
            this.list = data
        else
        {
            for(let v of data)
            this.list.push(v);
        }

        if(!this._listDataArr){
            this._listDataArr = new eui.ArrayCollection();
        }

        this._listDataArr.replaceAll(this.list);
        if(!this.rank_list.dataProvider){
            this.rank_list.dataProvider = this._listDataArr;
        }
    }

    public addEvent()
    {
        this.btn_close.addTapHandler(this.close, this);
        this.rank_scroller.addEventListener(eui.UIEvent.CHANGE_END, this.scrollerWorldChangeHandler, this);
        this.tab_1.addEventListener(egret.TouchEvent.TOUCH_TAP, this.changeUI.bind(this, [1]), this);
        this.tab_2.addEventListener(egret.TouchEvent.TOUCH_TAP, this.changeUI.bind(this, [2]), this);

        // this.btn_last.addTapHandler(this.LeftBtnOnClick, this);
        // this.btn_next.addTapHandler(this.RightBtnOnClick, this);
    }
     
    // protected LeftBtnOnClick()
    // {
    //     if(this.curPage <= 0)
    //     {
    //         MessageUtil.tips("到了最前面了哦");
    //         return;
    //     }
    //     this.curPage --;
    //     RankManager.instance().requestLastPageData();
    // }

    // protected RightBtnOnClick()
    // {
    //     this.curPage ++;
    //     RankManager.instance().requestNextPageData();
    // }

    protected scrollerWorldChangeHandler(e:egret.Event)
	{
		//滑动判定分页请求
		let target:eui.IViewport = e.currentTarget.viewport;
        if(target.scrollV >= target.measuredHeight - target.height && this.list.length < 100)
        {
            this.postWorldData();
        }
	}

    protected close()
    {
        this.remove(1);
    }

    public removeEvent()
    {
        this.btn_close.removeTapHandler();
        // this.btn_last.removeTapHandler();
        // this.btn_next.removeTapHandler();
        
        this.rank_scroller.removeEventListener(eui.UIEvent.CHANGE_END, this.scrollerWorldChangeHandler, this);
        this.tab_1.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.changeUI, this);
        this.tab_2.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.changeUI, this);
    }

    public clear()
    {
        super.clear();
        this.curPage=null;
        this._curPage = null;
        this.rank_list.itemRenderer = null;
        this.rank_list.dataProvider = null;
        this.list = null;
        this._listDataArr = null;
        this.curSelectIndex = null;
        UIManager.instance().showLamp();
    }
}